﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using YTSK.Model;
using System.Xml;
using System.Windows.Forms;

namespace YTSK.cSK
{
    public static class cMakeXML
    {
        /// <summary>
        /// 生成专票的xml文件
        /// </summary>
        public static string ZP(Model.SK_SalebillVouch sk, List<Model.SK_SalebillVouchs_View> l_sks,
            out string savefile)
        {
            savefile = "";
            cSKSet:
            var _FHR = cSK.cSKSet.loadsave(SkConfig.复核人);
            var _SKR = cSK.cSKSet.loadsave(SkConfig.收款人);
            var _version = cSK.cSKSet.loadsave(SkConfig.税控分类版本号);
            if (_version.Length == 0)
            {
                MessageBox.Show("请先设置税控设备参数");
                var f = new FrmSKSet();
                f.ShowDialog();
                goto cSKSet;
            }

            var xml = new XmlDocument();
            var declare = xml.CreateXmlDeclaration("1.0", "GBK", null);
            xml.AppendChild(declare);
            var kp = xml.CreateElement("Kp");
            var version = xml.CreateElement("Version");
            version.InnerText = "2.0";
            var fpxx = xml.CreateElement("Fpxx");
            var zsl = xml.CreateElement("Zsl");
            zsl.InnerText = "1";
            fpxx.AppendChild(zsl);
            var fpsj = xml.CreateElement("Fpsj");
            var fp = xml.CreateElement("Fp");
            var main = xml.CreateElement("Djh");
            if (sk.cCode.Contains("XSA"))
            {
                main.InnerText = $"{sk.cCode.Replace("XSA", "").Replace("-", "")}{DateTime.Now.ToString("HHmmss")}";
            }
            if (sk.cCode.Contains("XST"))
            {
                main.InnerText = $"{sk.cCode.Replace("XST", "").Replace("-", "")}{DateTime.Now.ToString("HHmmss")}";
            }
            fp.AppendChild(main);
            main = xml.CreateElement("Gfmc");
            main.InnerText = sk.cCusName;
            fp.AppendChild(main);
            main = xml.CreateElement("Gfsh");
            main.InnerText = sk.TaxNo;
            fp.AppendChild(main);
            main = xml.CreateElement("Gfyhzh");
            main.InnerText = sk.Bank.ObjToString() + " " + sk.BankAccount.ObjToString();
            fp.AppendChild(main);
            main = xml.CreateElement("Gfdzdh");
            main.InnerText = sk.Address.ObjToString() + " " + sk.Phone.ObjToString();
            fp.AppendChild(main);
            main = xml.CreateElement("Bz");
            main.InnerText = sk.cDf10.ObjToString();

            //main.InnerText = customer["Address"].ObjToString() + customer["Phone"].ObjToString();
            fp.AppendChild(main);
            main = xml.CreateElement("Fhr");
            main.InnerText = _FHR;
            fp.AppendChild(main);
            main = xml.CreateElement("Skr");
            main.InnerText = _SKR;
            fp.AppendChild(main);
            main = xml.CreateElement("Spbmbbh");
            main.InnerText = _version;
            fp.AppendChild(main);
            main = xml.CreateElement("Hsbz");
            main.InnerText = "0";
            fp.AppendChild(main);
            main = xml.CreateElement("Sgbz");
            main.InnerText = "0";
            fp.AppendChild(main);

            main = xml.CreateElement("Spxx");
            int irowcount = 1;
            for (int i = 0; i < l_sks.Count; i++)
            {
                var t = l_sks[i];
                if (t.cTaxType.ObjToString().Length != 19)
                {
                    return $"产品[{t.cInvName}]的税收编码[{t.cTaxType}]不是是19位";
                }

                if (t.cInvName.ObjToString().Length > 0)
                {
                    #region 原价行

                    var d = xml.CreateElement("Sph");
                    var detail = xml.CreateElement("Xh");
                    detail.InnerText = irowcount.ToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Spbm");
                    detail.InnerText = t.cTaxType;
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Qyspbm");
                    //detail.InnerText = "";
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Syyhzcbz");
                    //detail.InnerText = "";
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Yhzcsm");
                    //detail.InnerText = "";
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Lslbz");
                    //detail.InnerText = "";
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Spmc");
                    detail.InnerText = t.cInvName;
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Ggxh");
                    detail.InnerText = t.cInvStd;
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Jldw");
                    detail.InnerText = t.cDW;
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Dj");
                    detail.InnerText = (t.iQuoteMoney / t.iQuantity).ToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Sl");
                    detail.InnerText = t.iQuantity.ObjToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Je");
                    detail.InnerText = t.iQuoteMoney.ToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Se");
                    detail.InnerText = t.iQuoteTax.ToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Slv");
                    var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                    detail.InnerText = itaxrate.ToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("Kce");
                    d.AppendChild(detail);
                    main.AppendChild(d);
                    irowcount++;

                    #endregion

                    #region 折扣行

                    if (t.iDiscountMoney > 0)
                    {
                        d = xml.CreateElement("Sph");
                        detail = xml.CreateElement("Xh");
                        detail.InnerText = irowcount.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Spbm");
                        detail.InnerText = t.cTaxType;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Qyspbm");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Syyhzcbz");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Yhzcsm");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Lslbz");
                        //detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Spmc");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Ggxh");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Jldw");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Dj");
                        detail.InnerText = "0";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Sl");
                        detail.InnerText = "0";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Je");
                        detail.InnerText = (-t.iDiscountMoney).ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Se");
                        detail.InnerText = (-t.iDiscountTax).ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Slv");
                        itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        detail.InnerText = itaxrate.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("Kce");
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;
                    }

                    #endregion
                }
            }

            fp.AppendChild(main);
            fpsj.AppendChild(fp);
            kp.AppendChild(version);
            fpxx.AppendChild(fpsj);
            kp.AppendChild(fpxx);
            xml.AppendChild(kp);
            var path = System.Environment.GetEnvironmentVariable("TEMP");
            if (path.Substring(path.Length - 1, 1) != "\\")
            {
                path = path + "\\";
            }

            savefile = $"{path}{sk.cCode}.xml";
            if (System.IO.File.Exists(savefile))
            {
                System.IO.File.Delete(savefile);
            }

            xml.Save(savefile);
            // var destpath = "C:\\税控接口\\待开票\\"; //待开票目标文件夹
            // var destfile = $"{destpath}{sk.cCode}.xml";
            // System.IO.File.Copy(savefile, destfile);
            return "";
        }

        /// <summary>
        /// 生成电子票的XML文件-Ukey
        /// </summary>
        public static string DZP_Ukey(Model.SK_SalebillVouch sk, List<Model.SK_SalebillVouchs_View> l_sks, out string
            savefile, out string str_xml)
        {
            var path = System.Environment.GetEnvironmentVariable("TEMP");
            if (path.Substring(path.Length - 1, 1) != "\\")
            {
                path = path + "\\";
            }

            savefile = $"{path}{sk.cCode}.xml";
            ReGet:
            var _FHR = cSK.cSKSet.loadsave(SkConfig.复核人);
            var _SKR = cSK.cSKSet.loadsave(SkConfig.收款人);
            var _fpdz = cSK.cSKSet.loadsave(SkConfig.默认发票地址电话);
            var _yh = cSK.cSKSet.loadsave(SkConfig.默认银行及账号);
            var _version = cSK.cSKSet.loadsave(SkConfig.税控分类版本号);
            var _xsfmc = cSK.cSKSet.loadsave(SkConfig.销货单位名称);
            var _xsfnsrsbh = cSK.cSKSet.loadsave(SkConfig.销货单位识别码);
            if (_version.Length == 0)
            {
                MessageBox.Show("请先设置税控设备参数");
                var f = new FrmSKSet();
                f.ShowDialog();
                goto ReGet;
            }

            var xml = new XmlDocument();
            var declare = xml.CreateXmlDeclaration("1.0", "GBK", null);
            xml.AppendChild(declare);

            #region 表头定义

            var kp = xml.CreateElement("business");
            var attr = xml.CreateAttribute("id");
            attr.Value = "FPKJ";
            kp.Attributes.Append(attr);
            attr = xml.CreateAttribute("comment");
            attr.Value = "发票开具";
            kp.Attributes.Append(attr);

            var REQUEST_COMMON_FPKJ = xml.CreateElement("REQUEST_COMMON_FPKJ");
            attr = xml.CreateAttribute("class");
            attr.Value = "REQUEST_COMMON_FPKJ";
            REQUEST_COMMON_FPKJ.Attributes.Append(attr);
            var topmain = xml.CreateElement("COMMON_FPKJ_FPT");
            attr = xml.CreateAttribute("class");
            attr.Value = "COMMON_FPKJ_FPT";
            topmain.Attributes.Append(attr);

            #endregion

            #region 表头栏目

            var top = xml.CreateElement("FPQQLSH");
            top.InnerText = DateTime.Now.ToString("yyMMddhhmmss");
            topmain.AppendChild(top);
            top = xml.CreateElement("KPLX");
            top.InnerText = "0";
            topmain.AppendChild(top);
            top = xml.CreateElement("BMB_BBH");
            top.InnerText = _version;
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_NSRSBH");
            top.InnerText = _xsfnsrsbh;
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_MC");
            top.InnerText = _xsfmc;
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_DZDH");
            top.InnerText = _fpdz;
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_YHZH");
            top.InnerText = _yh;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_NSRSBH");
            top.InnerText = sk.TaxNo;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_MC");
            top.InnerText = sk.cCusName;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_DZDH");
            top.InnerText = string.Format("{0} {1}", sk.Address, sk.Phone);
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_YHZH");
            top.InnerText = string.Format("{0} {1}", sk.Bank, sk.BankAccount);
            topmain.AppendChild(top);
            top = xml.CreateElement("KPR");
            top.InnerText = "开票人";
            topmain.AppendChild(top);
            top = xml.CreateElement("SKR");
            top.InnerText = _SKR;
            topmain.AppendChild(top);
            top = xml.CreateElement("FHR");
            top.InnerText = _FHR;
            topmain.AppendChild(top);
            top = xml.CreateElement("YFP_DM");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("YFP_HM");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("HSBZ");
            top.InnerText = "0";
            topmain.AppendChild(top);
            // top = xml.CreateElement("JSHJ");
            // top.InnerText = "";
            // topmain.AppendChild(top);
            // top = xml.CreateElement("HJJE");
            // top.InnerText = "";
            // topmain.AppendChild(top);
            // top = xml.CreateElement("HJSE");
            // top.InnerText = "";
            // topmain.AppendChild(top);

            #endregion

            #region 明细开票

            var main = xml.CreateElement("COMMON_FPKJ_XMXXS");
            attr = xml.CreateAttribute("class");
            attr.Value = "COMMON_FPKJ_XMXX";
            main.Attributes.Append(attr);
            attr = xml.CreateAttribute("size");
            attr.Value = "1";
            main.Attributes.Append(attr);
            decimal sumjshj = 0;
            decimal sumje = 0;
            decimal sumse = 0;

            // string _YJH = "0"; //原价行
            // string _ZKH = "1"; //折扣行
            // string _BZKH = "2"; //被折扣行

            int irowcount = 1;
            for (int i = 0; i < l_sks.Count; i++)
            {
                var t = l_sks[i];
                if (t.cInvName.ObjToString().Length > 0)
                {
                    #region 原价行

                    if (t.iDiscountSum.ObjToDecimal() == 0)
                    {
                        var d = xml.CreateElement("COMMON_FPKJ_XMXX");
                        var cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        {
                            throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType));
                        }

                        var detail = xml.CreateElement("SPBM");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);

                        detail = xml.CreateElement("ZXBM");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("YHZCBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("ZZSTSGL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("LSLBS");
                        if (t.iTaxRateSale == 0)
                        {
                            detail.InnerText = "2";
                        }
                        else
                        {
                            detail.InnerText = "";
                        }

                        d.AppendChild(detail);
                        detail = xml.CreateElement("FPHXZ"); //发票行性质-原价行
                        detail.InnerText = "0";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMMC");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("GGXH");
                        detail.InnerText = t.cInvStd;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("DW");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        var jehs = t.iSumSale.ObjToDecimal();   //价税合计
                        var taxrate = t.iTaxRateSale.ObjToDecimal();
                        var jeori = jehs / (1 + taxrate / 100);
                        decimal jeinput = Math.Round(jeori, 2, MidpointRounding.AwayFromZero);
                        var se = jehs - jeinput;
                        var sl = t.iQuantity;   //数量
                        var dj = jeinput / sl;  //单价
                        var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        sumjshj += jehs;
                        sumje += jeinput;
                        sumse += se;
                        detail = xml.CreateElement("XMSL");
                        detail.InnerText = sl.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMDJ");
                        var s_djinput = dj.ToString();
                        detail.InnerText = s_djinput;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMHJ");
                        detail.InnerText = jehs.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMJE");
                        detail.InnerText = jeinput.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SL");
                        detail.InnerText = itaxrate.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SE");
                        detail.InnerText = se.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("KCE");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;
                    }

                    #endregion

                    #region 折扣

                    else
                    {
                        #region 原价行

                        var d = xml.CreateElement("COMMON_FPKJ_XMXX");
                        var cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        {
                            throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType));
                        }

                        var detail = xml.CreateElement("SPBM");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);

                        detail = xml.CreateElement("ZXBM");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("YHZCBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("ZZSTSGL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("LSLBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        d.AppendChild(detail);
                        detail = xml.CreateElement("FPHXZ"); //发票行性质-被折扣行
                        detail.InnerText = "2";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMMC");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("GGXH");
                        detail.InnerText = t.cInvStd;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("DW");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        var jehs = t.iQuoteSum.ObjToDecimal();
                        var taxrate = t.iTaxRateSale.ObjToDecimal();
                        var jeori = jehs / (1 + taxrate / 100);
                        decimal jeinput = Math.Round(jeori, 2, MidpointRounding.AwayFromZero);
                        var se = jehs - jeinput;
                        var sl = t.iQuantity;   //数量
                        var dj = jeinput / sl;  //单价
                        var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        sumjshj += jehs;
                        sumje += jeinput;
                        sumse += se;
                        detail = xml.CreateElement("XMSL");
                        detail.InnerText = sl.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMDJ");
                        var s_djinput = dj.ToString();
                        detail.InnerText = s_djinput;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMHJ");
                        detail.InnerText = jehs.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMJE");
                        detail.InnerText = jeinput.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SL");
                        detail.InnerText = itaxrate.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SE");
                        detail.InnerText = se.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("KCE");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;

                        #endregion

                        #region 折扣行

                        d = xml.CreateElement("COMMON_FPKJ_XMXX");
                        cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        {
                            throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType));
                        }

                        detail = xml.CreateElement("SPBM");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);

                        detail = xml.CreateElement("ZXBM");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("YHZCBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("ZZSTSGL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("LSLBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("FPHXZ"); // 发票行性质-折扣行
                        detail.InnerText = "1";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMMC");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("GGXH");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("DW");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        jehs = -t.iDiscountSum.ObjToDecimal();
                        taxrate = t.iTaxRateSale.ObjToDecimal();
                        jeori = jehs / (1 + taxrate / 100);
                        jeinput = Math.Round(jeori, 2, MidpointRounding.AwayFromZero);
                        se = jehs - jeinput;
                        itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        sumjshj += jehs;
                        sumje += jeinput;
                        sumse += se;
                        detail = xml.CreateElement("XMSL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMDJ");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMHJ");
                        detail.InnerText = jehs.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMJE");
                        detail.InnerText = jeinput.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SL");
                        detail.InnerText = itaxrate.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SE");
                        detail.InnerText = se.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("KCE");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;

                        #endregion
                    }

                    #endregion
                }
            }

            #endregion

            // sumje = Math.Round(sumje, 2);
            // sumse = Math.Round(sumse, 2);

            #region 表头项目增加

            top = xml.CreateElement("JSHJ");
            top.InnerText = sumjshj.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HJJE");
            top.InnerText = sumje.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HJSE");
            top.InnerText = sumse.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HSBZ");
            top.InnerText = "0";
            topmain.AppendChild(top);
            top = xml.CreateElement("BZ");
            top.InnerText = sk.cDf10;
            topmain.AppendChild(top);

            #endregion

            #region 完成XML文件

            REQUEST_COMMON_FPKJ.AppendChild(topmain);
            REQUEST_COMMON_FPKJ.AppendChild(main);
            kp.AppendChild(REQUEST_COMMON_FPKJ);
            xml.AppendChild(kp);

            #endregion

            #region XML转string
            
            using (var stringWriter = new StringWriter())
            {
                using (var xmlTextWriter = XmlWriter.Create(stringWriter))
                {
                    xml.WriteTo(xmlTextWriter);
                    xmlTextWriter.Flush();
                    str_xml = stringWriter.GetStringBuilder().ToString();
                }
            }

            #endregion
            
            #region 保存文件

            // if (System.IO.File.Exists(savefile))
            // {
            //     System.IO.File.Delete(savefile);
            // }
            //
            // xml.Save(savefile);

            #endregion

            return "";
        }


        /// <summary>
        /// 生成电子票的XML文件-航天新版
        /// </summary>
        public static string DZP_HTNew(Model.SK_SalebillVouch sk, List<Model.SK_SalebillVouchs_View> l_sks, out string
            savefile)
        {
            var path = System.Environment.GetEnvironmentVariable("TEMP");
            if (path.Substring(path.Length - 1, 1) != "\\")
            {
                path = path + "\\";
            }

            savefile = $"{path}{sk.cCode}.xml";
            ReGet:
            var _FHR = cSK.cSKSet.loadsave(SkConfig.复核人);
            var _SKR = cSK.cSKSet.loadsave(SkConfig.收款人);
            var _fpdz = cSK.cSKSet.loadsave(SkConfig.默认发票地址电话);
            var _yh = cSK.cSKSet.loadsave(SkConfig.默认银行及账号);
            var _version = cSK.cSKSet.loadsave(SkConfig.税控分类版本号);
            var _xsfmc = cSK.cSKSet.loadsave(SkConfig.销货单位名称);
            var _xsfnsrsbh = cSK.cSKSet.loadsave(SkConfig.销货单位识别码);
            if (_version.Length == 0)
            {
                MessageBox.Show("请先设置税控设备参数");
                var f = new FrmSKSet();
                f.ShowDialog();
                goto ReGet;
            }

            var xml = new XmlDocument();
            var declare = xml.CreateXmlDeclaration("1.0", "GBK", null);
            xml.AppendChild(declare);

            #region 表头定义

            var kp = xml.CreateElement("business");
            var attr = xml.CreateAttribute("id");
            attr.Value = "FPKJ";
            kp.Attributes.Append(attr);
            attr = xml.CreateAttribute("comment");
            attr.Value = "发票开具";
            kp.Attributes.Append(attr);

            var REQUEST_COMMON_FPKJ = xml.CreateElement("REQUEST_COMMON_FPKJ");
            attr = xml.CreateAttribute("class");
            attr.Value = "REQUEST_COMMON_FPKJ";
            REQUEST_COMMON_FPKJ.Attributes.Append(attr);
            var topmain = xml.CreateElement("COMMON_FPKJ_FPT");
            attr = xml.CreateAttribute("class");
            attr.Value = "COMMON_FPKJ_FPT";
            topmain.Attributes.Append(attr);

            #endregion

            #region 表头栏目

            var top = xml.CreateElement("FPQQLSH");
            top.InnerText = DateTime.Now.ToString("yyMMddhhmmss");
            topmain.AppendChild(top);
            top = xml.CreateElement("KPLX");
            top.InnerText = "0";
            topmain.AppendChild(top);
            top = xml.CreateElement("BMB_BBH");
            top.InnerText = _version;
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_NSRSBH");
            top.InnerText = _xsfnsrsbh;
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_MC");
            top.InnerText = _xsfmc;
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_DZDH");
            top.InnerText = _fpdz;
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_YHZH");
            top.InnerText = _yh;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_NSRSBH");
            top.InnerText = sk.TaxNo;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_MC");
            top.InnerText = sk.cCusName;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_DZDH");
            top.InnerText = string.Format("{0} {1}", sk.Address, sk.Phone);
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_YHZH");
            top.InnerText = string.Format("{0} {1}", sk.Bank, sk.BankAccount);
            topmain.AppendChild(top);
            top = xml.CreateElement("KPR");
            top.InnerText = "开票人";
            topmain.AppendChild(top);
            top = xml.CreateElement("SKR");
            top.InnerText = _SKR;
            topmain.AppendChild(top);
            top = xml.CreateElement("FHR");
            top.InnerText = _FHR;
            topmain.AppendChild(top);
            top = xml.CreateElement("YFP_DM");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("YFP_HM");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("HSBZ");
            top.InnerText = "0";
            topmain.AppendChild(top);

            #endregion

            #region 明细开票

            var main = xml.CreateElement("COMMON_FPKJ_XMXXS");
            attr = xml.CreateAttribute("class");
            attr.Value = "COMMON_FPKJ_XMXX";
            main.Attributes.Append(attr);
            attr = xml.CreateAttribute("size");
            attr.Value = "1";
            main.Attributes.Append(attr);
            decimal sumjshj = 0;
            decimal sumje = 0;
            decimal sumse = 0;

            // string _YJH = "0"; //原价行
            // string _ZKH = "1"; //折扣行
            // string _BZKH = "2"; //被折扣行

            int irowcount = 1;
            for (int i = 0; i < l_sks.Count; i++)
            {
                var t = l_sks[i];
                if (t.cInvName.ObjToString().Length > 0)
                {
                    #region 原价行

                    if (t.iDiscountSum.ObjToDecimal() == 0)
                    {
                        var d = xml.CreateElement("COMMON_FPKJ_XMXX");
                        var cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        {
                            throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType));
                        }

                        var detail = xml.CreateElement("SPBM");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);

                        detail = xml.CreateElement("ZXBM");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("YHZCBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("ZZSTSGL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("LSLBS");
                        if (t.iTaxRateSale == 0)
                        {
                            detail.InnerText = "2";
                        }
                        else
                        {
                            detail.InnerText = "";
                        }

                        d.AppendChild(detail);
                        detail = xml.CreateElement("FPHXZ"); //发票行性质-原价行
                        detail.InnerText = "0";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMMC");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("GGXH");
                        detail.InnerText = t.cInvStd;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("DW");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        var jehs = t.iSumSale.ObjToDecimal();
                        var taxrate = t.iTaxRateSale.ObjToDecimal();
                        var jeori = jehs / (1 + taxrate / 100);
                        decimal jeinput = Math.Round(jeori, 2, MidpointRounding.AwayFromZero);
                        var se = jehs - jeinput;
                        var sl = t.iQuantity;   //数量
                        var dj = jeinput / sl;//单价
                        var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        sumjshj += jehs;
                        sumje += jeinput;
                        sumse += se;
                        detail = xml.CreateElement("XMSL");
                        detail.InnerText = sl.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMDJ");
                        var s_djinput = dj.ToString();
                        detail.InnerText = s_djinput;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMHJ");
                        detail.InnerText = jehs.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMJE");
                        detail.InnerText = jeinput.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SL");
                        detail.InnerText = itaxrate.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SE");
                        detail.InnerText = se.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("KCE");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;
                    }

                    #endregion

                    #region 折扣

                    else
                    {
                        #region 原价行

                        var d = xml.CreateElement("COMMON_FPKJ_XMXX");
                        var cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        {
                            throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType));
                        }

                        var detail = xml.CreateElement("SPBM");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);

                        detail = xml.CreateElement("ZXBM");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("YHZCBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("ZZSTSGL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("LSLBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        d.AppendChild(detail);
                        detail = xml.CreateElement("FPHXZ"); //发票行性质-被折扣行
                        detail.InnerText = "2";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMMC");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("GGXH");
                        detail.InnerText = t.cInvStd;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("DW");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        var jehs = t.iQuoteSum.ObjToDecimal();  //折前价税合计--金额核算
                        var taxrate = t.iTaxRateSale.ObjToDecimal();
                        var jeori = jehs / (1 + taxrate / 100);  //不含税金额
                        decimal jeinput = Math.Round(jeori, 2, MidpointRounding.AwayFromZero);
                        var se = jehs - jeinput;
                        var sl = t.iQuantity;   //数量
                        var dj = jeinput / sl;  //单价
                        var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        sumjshj += jehs;
                        sumje += jeinput;
                        sumse += se;
                        detail = xml.CreateElement("XMSL");
                        detail.InnerText = sl.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMDJ");
                        var s_djinput = dj.ToString();
                        detail.InnerText = s_djinput;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMHJ");
                        detail.InnerText = jehs.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMJE");
                        detail.InnerText = jeinput.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SL");
                        detail.InnerText = itaxrate.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SE");
                        detail.InnerText = se.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("KCE");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;

                        #endregion

                        #region 折扣行

                        d = xml.CreateElement("COMMON_FPKJ_XMXX");
                        cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        {
                            throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType));
                        }

                        detail = xml.CreateElement("SPBM");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);

                        detail = xml.CreateElement("ZXBM");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("YHZCBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("ZZSTSGL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("LSLBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("FPHXZ"); // 发票行性质-折扣行
                        detail.InnerText = "1";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMMC");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("GGXH");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("DW");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        jehs = -t.iDiscountSum.ObjToDecimal();
                        taxrate = t.iTaxRateSale.ObjToDecimal();
                        jeori = jehs / (1 + taxrate / 100);
                        jeinput = Math.Round(jeori, 2, MidpointRounding.AwayFromZero);
                        se = jehs - jeinput;
                        itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        sumjshj += jehs;
                        sumje += jeinput;
                        sumse += se;
                        detail = xml.CreateElement("XMSL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMDJ");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMHJ");
                        detail.InnerText = jehs.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMJE");
                        detail.InnerText = jeinput.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SL");
                        detail.InnerText = itaxrate.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SE");
                        detail.InnerText = se.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("KCE");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;

                        #endregion
                    }

                    #endregion
                }
            }

            #endregion
            

            #region 表头项目增加

            top = xml.CreateElement("JSHJ");
            top.InnerText = sumjshj.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HJJE");
            top.InnerText = sumje.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HJSE");
            top.InnerText = sumse.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HSBZ");
            top.InnerText = "0";
            topmain.AppendChild(top);
            top = xml.CreateElement("BZ");
            top.InnerText = sk.cDf10;
            topmain.AppendChild(top);

            #endregion

            #region 完成XML文件

            REQUEST_COMMON_FPKJ.AppendChild(topmain);
            REQUEST_COMMON_FPKJ.AppendChild(main);
            kp.AppendChild(REQUEST_COMMON_FPKJ);
            xml.AppendChild(kp);

            #endregion

            #region 保存文件

            if (System.IO.File.Exists(savefile))
            {
                System.IO.File.Delete(savefile);
            }

            xml.Save(savefile);

            #endregion

            return "";
        }


        /// <summary>
        /// 生成电子票的XML文件
        /// </summary>
        public static string DZP_HT(Model.SK_SalebillVouch sk, List<Model.SK_SalebillVouchs_View> l_sks,
            out string savefile)
        {
            var path = System.Environment.GetEnvironmentVariable("TEMP");
            if (path.Substring(path.Length - 1, 1) != "\\")
            {
                path = path + "\\";
            }

            savefile = $"{path}{sk.cCode}.xml";
            ReGet:
            var _FHR = cSK.cSKSet.loadsave(SkConfig.复核人);
            var _SKR = cSK.cSKSet.loadsave(SkConfig.收款人);
            var _fpdz = cSK.cSKSet.loadsave(SkConfig.默认发票地址电话);
            var _yh = cSK.cSKSet.loadsave(SkConfig.默认银行及账号);
            var _version = cSK.cSKSet.loadsave(SkConfig.税控分类版本号);
            if (_version.Length == 0)
            {
                MessageBox.Show("请先设置税控设备参数");
                var f = new FrmSKSet();
                f.ShowDialog();
                goto ReGet;
            }

            var xml = new XmlDocument();
            var declare = xml.CreateXmlDeclaration("1.0", "GBK", null);
            xml.AppendChild(declare);

            #region 表头定义

            var kp = xml.CreateElement("business");
            var attr = xml.CreateAttribute("id");
            attr.Value = "FPKJ";
            kp.Attributes.Append(attr);
            attr = xml.CreateAttribute("comment");
            attr.Value = "发票开具";
            kp.Attributes.Append(attr);

            var REQUEST_COMMON_FPKJ = xml.CreateElement("REQUEST_COMMON_FPKJ");
            attr = xml.CreateAttribute("class");
            attr.Value = "REQUEST_COMMON_FPKJ";
            var topmain = xml.CreateElement("COMMON_FPKJ_FPT");
            attr = xml.CreateAttribute("class");
            attr.Value = "COMMON_FPKJ_FPT";
            topmain.Attributes.Append(attr);

            #endregion

            #region 表头栏目

            var top = xml.CreateElement("FPQQLSH");
            top.InnerText = DateTime.Now.ToString("yyMMddhhmmss");
            topmain.AppendChild(top);
            top = xml.CreateElement("KPLX");
            top.InnerText = "0";
            topmain.AppendChild(top);
            top = xml.CreateElement("BMB_BBH");
            top.InnerText = _version;
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_NSRSBH");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_MC");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_DZDH");
            top.InnerText = _fpdz;
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_YHZH");
            top.InnerText = _yh;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_NSRSBH");
            top.InnerText = sk.TaxNo;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_MC");
            top.InnerText = sk.cCusName;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_DZDH");
            top.InnerText = string.Format("{0} {1}", sk.Address, sk.Phone);
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_YHZH");
            top.InnerText = string.Format("{0} {1}", sk.Bank, sk.BankAccount);
            topmain.AppendChild(top);
            top = xml.CreateElement("KPR");
            top.InnerText = "开票人";
            topmain.AppendChild(top);
            top = xml.CreateElement("SKR");
            top.InnerText = _SKR;
            topmain.AppendChild(top);
            top = xml.CreateElement("FHR");
            top.InnerText = _FHR;
            topmain.AppendChild(top);
            top = xml.CreateElement("YFP_DM");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("YFP_HM");
            top.InnerText = "";
            topmain.AppendChild(top);

            #endregion

            #region 明细开票

            var main = xml.CreateElement("COMMON_FPKJ_XMXXS");
            attr = xml.CreateAttribute("class");
            attr.Value = "COMMON_FPKJ_XMXX";
            main.Attributes.Append(attr);
            attr = xml.CreateAttribute("size");
            attr.Value = "1";
            main.Attributes.Append(attr);
            decimal sumjshj = 0;
            decimal sumje = 0;
            decimal sumse = 0;

            int irowcount = 1;
            for (int i = 0; i < l_sks.Count; i++)
            {
                var t = l_sks[i];
                if (t.cInvName.ObjToString().Length > 0)
                {
                    #region 原价行

                    if (t.iDiscountSum.ObjToDecimal() == 0)
                    {
                        var d = xml.CreateElement("COMMON_FPKJ_XMXX");
                        var cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        {
                            throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType));
                        }

                        var detail = xml.CreateElement("SPBM");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);

                        detail = xml.CreateElement("ZXBM");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("YHZCBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("ZZSTSGL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("LSLBS");
                        if (t.iTaxRateSale == 0)
                        {
                            detail.InnerText = "2";
                        }
                        else
                        {
                            detail.InnerText = "";
                        }

                        d.AppendChild(detail);
                        detail = xml.CreateElement("FPHXZ");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMMC");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("GGXH");
                        detail.InnerText = t.cInvStd;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("DW");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        var jehs = t.iSumSale.ObjToDecimal();
                        var taxrate = t.iTaxRateSale.ObjToDecimal();
                        var jeori = jehs / (1 + taxrate / 100);
                        decimal jeinput = Math.Round(jeori, 16, MidpointRounding.AwayFromZero);
                        jeinput = Math.Round(jeinput, 2);
                        var se = jehs - jeinput;
                        var sl = t.iQuantity;
                        var dj = jeinput / sl;
                        var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        sumjshj += jehs;
                        sumje += jeinput;
                        sumse += se;
                        detail = xml.CreateElement("XMSL");
                        detail.InnerText = sl.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMDJ");
                        var s_djinput = dj.ToString();
                        detail.InnerText = s_djinput;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMHJ");
                        detail.InnerText = jehs.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMJE");
                        detail.InnerText = jeinput.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SL");
                        detail.InnerText = itaxrate.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SE");
                        detail.InnerText = se.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("KCE");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;
                    }

                    #endregion

                    #region 折扣

                    else
                    {
                        #region 原价行

                        var d = xml.CreateElement("COMMON_FPKJ_XMXX");
                        var cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        {
                            throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType));
                        }

                        var detail = xml.CreateElement("SPBM");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);

                        detail = xml.CreateElement("ZXBM");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("YHZCBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("ZZSTSGL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("LSLBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("FPHXZ");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMMC");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("GGXH");
                        detail.InnerText = t.cInvStd;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("DW");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        var jehs = t.iQuoteSum.ObjToDecimal();
                        var taxrate = t.iTaxRateSale.ObjToDecimal();
                        var jeori = jehs / (1 + taxrate / 100);
                        decimal jeinput = Math.Round(jeori, 16, MidpointRounding.AwayFromZero);
                        var se = jehs - jeinput;
                        var sl = t.iQuantity;
                        var dj = jeinput / sl;
                        var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        sumjshj += jehs;
                        sumje += jeinput;
                        sumse += se;
                        detail = xml.CreateElement("XMSL");
                        detail.InnerText = sl.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMDJ");
                        var s_djinput = dj.ToString();
                        detail.InnerText = s_djinput;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMHJ");
                        detail.InnerText = jehs.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMJE");
                        detail.InnerText = jeinput.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SL");
                        detail.InnerText = itaxrate.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SE");
                        detail.InnerText = se.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("KCE");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;

                        #endregion

                        #region 折扣行

                        d = xml.CreateElement("COMMON_FPKJ_XMXX");
                        cTaxType = t.cTaxType;
                        if (cTaxType.Length != 19)
                        {
                            throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType));
                        }

                        detail = xml.CreateElement("SPBM");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);

                        detail = xml.CreateElement("ZXBM");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("YHZCBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("ZZSTSGL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("LSLBS");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("FPHXZ");
                        detail.InnerText = cTaxType;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMMC");
                        detail.InnerText = t.cInvName;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("GGXH");
                        detail.InnerText = t.cInvStd;
                        d.AppendChild(detail);
                        detail = xml.CreateElement("DW");
                        detail.InnerText = t.cDW;
                        d.AppendChild(detail);
                        jehs = -t.iDiscountSum.ObjToDecimal();
                        taxrate = t.iTaxRateSale.ObjToDecimal();
                        jeori = jehs / (1 + taxrate / 100);
                        jeinput = Math.Round(jeori, 16, MidpointRounding.AwayFromZero);
                        se = jehs - jeinput;
                        //sl = 0;
                        //dj =
                        //djinput = Math.Round(dj.ObjToDecimal() , 16);
                        itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                        sumjshj += jehs;
                        sumje += jeinput;
                        sumse += se;
                        detail = xml.CreateElement("XMSL");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMDJ");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMHJ");
                        detail.InnerText = jehs.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("XMJE");
                        detail.InnerText = jeinput.ToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SL");
                        detail.InnerText = itaxrate.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("SE");
                        detail.InnerText = se.ObjToString();
                        d.AppendChild(detail);
                        detail = xml.CreateElement("KCE");
                        detail.InnerText = "";
                        d.AppendChild(detail);
                        main.AppendChild(d);
                        irowcount++;

                        #endregion
                    }

                    #endregion
                }
            }

            #endregion

            sumje = Math.Round(sumje, 2);
            sumse = Math.Round(sumse, 2);

            #region 表头项目增加

            top = xml.CreateElement("JSHJ");
            top.InnerText = sumjshj.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HJJE");
            top.InnerText = sumje.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HJSE");
            top.InnerText = sumse.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HSBZ");
            top.InnerText = "0";
            topmain.AppendChild(top);
            top = xml.CreateElement("BZ");
            top.InnerText = sk.cDf10;
            topmain.AppendChild(top);

            #endregion

            #region 完成XML文件

            REQUEST_COMMON_FPKJ.AppendChild(topmain);
            REQUEST_COMMON_FPKJ.AppendChild(main);
            kp.AppendChild(REQUEST_COMMON_FPKJ);
            xml.AppendChild(kp);

            #endregion

            #region 保存文件

            if (System.IO.File.Exists(savefile))
            {
                System.IO.File.Delete(savefile);
            }

            xml.Save(savefile);
            return "";

            #endregion
        }
    }
}